/* @(#)GSOrganizationProcessor.java    
 *
 * Project: GS
 *
 * Modify Information:
 * =============================================================================
 *   Author       Date       Description
 *   ------------ ---------- ---------------------------------------------------
 *   user 2006-2-14
 *   Jenny li    2006-3-10   添加方法getOrgAndSubOrg
 *   Simonyi Peng 2006-3-10  添加方法getOrgAndOper
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2006 Beijing HiTRUST Technology Co., Ltd. 1808 Room, 
 *        Science & Technology Building, No. 9 South Street, Zhong Guan Cun, 
 *        Haidian District, Beijing ,100081, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with HiTRUST.
 *
 * Warning:
 * =============================================================================
 * 
 */
package com.hitrust.gs.business;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;


import com.hitrust.gs.service.GSBaseProcesser;
import com.hitrust.gs.util.exception.GSException;
import com.hitrust.gs.bean.GSOrganizationBean;
import com.hitrust.gs.dao.GSOrganizationDAO;
import com.hitrust.gs.lib.GSLogWriter;
/**
 * 
 */
public class GSOrganizationProcessor implements GSBaseProcesser{

    /**
     * 通过机构编号和机构名称查询所有机构
     * 
     * @param aOrgId
     *            机构编号
     * @param aOrgName
     *            机构名称
     * @throws GSException          
     */
    public static ArrayList queryManageOrg(String aOrgId,String aOrgName)throws GSException{
        GSOrganizationDAO tGSOrganizationDAO = new GSOrganizationDAO();
        ArrayList tArrayList=new ArrayList();
        try {
            //通过机构编号和机构名称查询所有机构信息
            tArrayList=tGSOrganizationDAO.queryOrg(aOrgId,aOrgName);
        } catch (GSException e) {
            GSLogWriter.error("GSOrganizationProcessor GSException: " +e.toString());
        }
        return tArrayList;
    }
    
    /**
     * 找到所有运营公司和巴士物资的机构代码
     * 
     * 
     * @throws GSException          
     */
    public static ArrayList queryManageUpOrg()throws GSException{
        GSOrganizationDAO tGSOrganizationDAO = new GSOrganizationDAO();
        ArrayList tArrayList=new ArrayList();
        try {
            //通过机构编号和机构名称查询所有机构信息
            tArrayList=tGSOrganizationDAO.queryUpOrg();
        } catch (GSException e) {
            GSLogWriter.error("GSOrganizationProcessor GSException: " +e.toString());
        }
        return tArrayList;
    }
    /**
     * 新增机构信息
     * 
     *        
     */
    public static void addTask(GSOrganizationBean aGSOrganizationBean)throws GSException {
        GSOrganizationDAO tGSOrganizationDAO = new GSOrganizationDAO();
            //判断机构是否存在
            if (tGSOrganizationDAO.isExist(aGSOrganizationBean.getGs_org_id())) {
                          throw new GSException("该机构已经存在");
                }
            //判断上级组织机构编号是否存在
            if(tGSOrganizationDAO.isExist(aGSOrganizationBean.getGs_upper_orgId())==false){
                          throw new GSException("该上级组织机构不存在");
            }
            //00XX
            String tOrgId1=aGSOrganizationBean.getGs_org_id().substring(0,2);
            //XX00
            String tOrgId2=aGSOrganizationBean.getGs_org_id().substring(2,4);
            
            String tUorgId1=aGSOrganizationBean.getGs_upper_orgId().substring(2,4);
            
            String tUorgId2=aGSOrganizationBean.getGs_upper_orgId();
            
            String tUorgId3=aGSOrganizationBean.getGs_upper_orgId().substring(0,2);
            //判断上级组织机构编号是否有效
            if(tOrgId1.equals("00")||tOrgId2.equals("00")){
              if(!((tOrgId1.equals("00")||tOrgId2.equals("00"))&&tUorgId2.equals("0000"))){
                 throw new GSException("该上级组织机构必须是0000！");
              }
            }else{
                if((tUorgId3.equals("00"))==true){
                  throw new GSException("该上级组织机构必须是XX00！");
                }
            }
            //添加机构信息
            if (tGSOrganizationDAO.add(aGSOrganizationBean) == false) {
                throw new GSException("");
            }
        }
    
    /**
     * 通过机构编号得到详细信息
     * 
     * @param aOrgId
     *            机构编号
     * @throws GSException          
     */
    public static GSOrganizationBean getOrg(String aOrgId)throws GSException{
        GSOrganizationDAO tGSOrganizationDAO = new GSOrganizationDAO();
        GSOrganizationBean tGSOrganizationBean=new GSOrganizationBean();  
        try {
            //通过机构编号和机构名称查询所有机构信息
            tGSOrganizationBean=tGSOrganizationDAO.getOrg(aOrgId);
        } catch (GSException e) {
            GSLogWriter.error("GSOrganizationProcessor GSException: " +e.toString());
        }
        return tGSOrganizationBean;
    }
    /**
     * 修改机构信息
     * 
     * @param aMerchantTempBean
     *           
     * throws CUPSException
     */
    public static void modifyTask(GSOrganizationBean aGSOrganizationBean) throws GSException{
        GSOrganizationDAO tGSOrganizationDAO = new GSOrganizationDAO();
        
        //判断上级组织机构编号是否存在
        if(tGSOrganizationDAO.isExist(aGSOrganizationBean.getGs_upper_orgId())==false){
                      throw new GSException("该上级组织机构不存在");
        }
        //00XX
        String tOrgId1=aGSOrganizationBean.getGs_org_id().substring(0,2);
        //XX00
        String tOrgId2=aGSOrganizationBean.getGs_org_id().substring(2,4);
        
        String tUorgId1=aGSOrganizationBean.getGs_upper_orgId().substring(2,4);
        
        String tUorgId2=aGSOrganizationBean.getGs_upper_orgId();
        
        String tUorgId3=aGSOrganizationBean.getGs_upper_orgId().substring(0,2);
        //判断上级组织机构编号是否有效
        if(tOrgId1.equals("00")||tOrgId2.equals("00")){
          if(!((tOrgId1.equals("00")||tOrgId2.equals("00"))&&tUorgId2.equals("0000"))){
             throw new GSException("该上级组织机构必须是0000！");
          }
        }else{
            if((tUorgId3.equals("00"))==true){
              throw new GSException("该上级组织机构必须是XX00！");
            }
        }
        //修改机构信息
        if(tGSOrganizationDAO.modify(aGSOrganizationBean)==false){
            throw new GSException("");
        }
    }
    
    /**
     * 删除组织机构信息
     * 
     * @param aOrgId 机构编号
     *            
     * throws CUPSException
     */
    public static void withdrawTask(String aOrgId)throws GSException{
        GSOrganizationDAO tGSOrganizationDAO = new GSOrganizationDAO();
       
         //删除组织机构信息
            if(tGSOrganizationDAO.delete(aOrgId)==false){
                throw new GSException("");
            }
         //设置对应的操作员为无效
            if(tGSOrganizationDAO.setOperValidity(aOrgId)==false){
				//throw new GSException("");
            }
    }
    /**
     * 根据已有的一级组织和全部组织信息，封装一级组织对应的所有二级组织
     * @param aUpperOrg                一级组织信息
     * @param allOrg                   所有组织信息     
     * @return
     */
    public HashMap getOrgAndSubOrg(ArrayList aUpperOrg,ArrayList allOrg){
        //取得上级组织机构编号
        
        HashMap tMap = new HashMap();
        
        if(aUpperOrg != null ){
            for(int i=0; i<aUpperOrg.size(); i++){
                //上级组织机构编号
                GSOrganizationBean tGSOrganizationBean = (GSOrganizationBean)aUpperOrg.get(i);
                
                if(allOrg != null){
                    ArrayList tList1 = new ArrayList();
                    for(int j=0; j<allOrg.size();j++){
                        //二级组织
                        GSOrganizationBean tGSOrganizationBean1 = (GSOrganizationBean)allOrg.get(j);
                        String level1=tGSOrganizationBean.getGs_org_id();
                        String level2=tGSOrganizationBean1.getGs_upper_orgId();
                        String orgid=tGSOrganizationBean1.getGs_org_id();
                       
                        if(level1.equals("0000")){
                           //判断巴士物资的下拉
                          if(level1.equals(level2)&&orgid.substring(0,2).equals("00")){
                             tList1.add(tGSOrganizationBean1);
                          }
                        }
                        else{
                           //运营公司的下拉判断
                            if(level1.equals(level2)||orgid.equals(level1)){
                                tList1.add(tGSOrganizationBean1);
                            }
                        }
                      
                    }
                    if(tList1.size() > 0)
                        tMap.put(""+tGSOrganizationBean.getGs_org_id(),tList1);
                }
            }
        }
        return tMap;
    }
    
    /**
     * 获得所有组织机构以及下属的操作员
     * @return arraylist，第一个成员是组织机构map，第二个是操作员map
     * @throws GSException
     */
    public ArrayList getOrgAndOper() throws GSException {
        GSOrganizationDAO tGSOrganizationDAO = new GSOrganizationDAO();
        return tGSOrganizationDAO.getOrgAndOper();
    }
    /***
     * 获得所有运行公司信息
     * */
    public ArrayList getOrgWarehouse() throws GSException
    {
        GSOrganizationDAO tGSOrganizationDAO = new GSOrganizationDAO();
        Connection tConnection = tGSOrganizationDAO.getConnetion();
        ArrayList arr = null;
        try{
            arr= tGSOrganizationDAO.queryAllOrganization2(tConnection);
        }catch(Exception e) {
            GSLogWriter.error("没有查询到机构信息！"+e.getMessage());
        }finally{
            GSOrganizationDAO.close(tConnection);
        }
        
        if(arr != null && arr.size()<=0){
            GSLogWriter.error("没有查询到机构信息！");
        }
        
        return arr;
    }
    /**
     * 获取自己及下级机构
     * */
    public ArrayList getOrgSub(String orgid) throws GSException{
        GSOrganizationDAO tGSOrganizationDAO = new GSOrganizationDAO();
        Connection tConnection = tGSOrganizationDAO.getConnetion();
        ArrayList arr =null;
        try{
            arr=tGSOrganizationDAO.getSubOrg(tConnection, orgid);
            if(arr == null)
                arr = new ArrayList();
            
            arr.add(tGSOrganizationDAO.getOrg(tConnection,orgid));
            
        }catch(Exception e){
            GSLogWriter.error("没有查询到自己及下级机构信息！"+e.getMessage());
        }finally{
            GSOrganizationDAO.close(tConnection);
        }
        
        if(arr != null && arr.size()<=0){
            GSLogWriter.error("没有查询到自己及下级机构信息！");
        }
        return arr;
    }
}
